//
//  HistoryViewController.m
//  MyNewProject
//
//  Created by sun on 2018/12/3.
//  Copyright © 2018 sun. All rights reserved.
//

#import "HistoryViewController.h"
#import "XMHistoryCollectionViewCell.h"
#import "XRWaterfallLayout.h"
#import "ProductDetailViewController.h"

#import "XM_AuctionProductModel.h"
@interface HistoryViewController ()<UICollectionViewDataSource,XRWaterfallLayoutDelegate,UICollectionViewDelegate>
{

}
@property(nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *indexArray;

@end


static NSString *const superCollectionViewCellID = @"XMHistoryCollectionViewCell";
@implementation HistoryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navigationBar.hidden = NO;
    self.gk_navTitle = @"历史";
    [self.view addSubview:self.collectionView];
    [self requestData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
#pragma - mark 网络请求
-(void)requestData{
    [YQNetworking postWithUrl:@"index/getLastSellList" refreshRequest:YES cache:NO params:@{} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {

    } successBlock:^(id response) {
        [self.collectionView.mj_header endRefreshing];
        [self.dataArray removeAllObjects];
        NSArray * dataArray = response[@"data"];
        for (NSDictionary * dic in dataArray) {
            XM_AuctionProductModel * pModel =  [XM_AuctionProductModel modelWithDictionary:dic];
            if (pModel.status == 5) {
                pModel.status = 3;
            }
            [self.dataArray addObject:pModel];
        }
        [self.collectionView reloadData];
    } failBlock:^(NSError *error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}
-(void)requestMoreData{

}
#pragma - mark Setter
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        //创建瀑布流布局
        XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
        //或者一次性设置
        [waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
        //设置代理，实现代理方法
        waterfall.delegate = self;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, SKTopBar_Height, SCREEN_WIDTH, SCREENH_HEIGHT - SKTabbarHeight - SKBottomSafe_Height - SKTopBar_Height) collectionViewLayout:waterfall];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[XMHistoryCollectionViewCell class] forCellWithReuseIdentifier:superCollectionViewCellID];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self requestData];
        }];

        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _collectionView;
}
#pragma -mark XRWaterfallLayoutDelegate
//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
    //根据数据 展示不同的高度
    return itemWidth + 80;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMHistoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:superCollectionViewCellID forIndexPath:indexPath];
    cell.countDownTimelab.hidden = YES;
    cell.timeLabel.hidden = NO;
    
    [cell LoadProductInfomation:self.dataArray[indexPath.row] isShop:NO];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XM_AuctionProductModel * pModel = self.dataArray[indexPath.row];
    ProductDetailViewController * pdVC = [[ProductDetailViewController alloc]init];
    pdVC.type = pModel.status;//1未开始2进行中3已结束4已成交5积分
    pdVC.productId = pModel.productId;
    [self.navigationController pushViewController:pdVC animated:YES];
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
